import {
  repairExportUrl,
  downloadTemplateUrl,
  getIsReviewList,
  getCellList,
  getRepairProcessList,
  commonBaseEquipmentModelingList
} from "@/api/review/mesScanRecordDefective"

import { exportExcel } from "@/utils/judge"
//工序, 工位, 设备
const optionList = [[], [], []]
// 列表头按钮配置
export const operateList = [
  {
    name: $i18n.t("common.export"),
    type: "primary",
    icon: "el-icon-download",
    handleName: "export", //点击时返回的标识
    url: repairExportUrl
    //auth: "auth:operlog:export", //权限字段
  },
  {
    name: $i18n.t("repairMaterialsInfo.repair"),
    type: "primary",
    icon: "el-icon-circle-plus-outline",
    handleName: "createReview", //点击时返回的标识
    isMultiple: true // isSingle 表示必须选择一项//isMultiple: true, // 表示必须多选
  }
  // {
  //   name: $i18n.t("common.delete"),
  //   type: "danger",
  //   icon: "el-icon-delete",
  //   handleName: "deleteBatch", //点击时返回的标识
  //   isMultiple: true, // isSingle 表示必须选择一项//isMultiple: true, // 表示必须多选
  // },
  // {
  //   name: "导入",
  //   type: "primary",
  //   icon: "el-icon-circle-plus-outline",
  //   handleName: "import", //点击时返回的标识
  // },
]
// 行末按钮配置
export const rowConfig = [
  {
    name: $i18n.t("common.edit"),
    type: "primary",
    class: "row-btn-primary",
    handleName: "edit" //点击时返回的标识
  },
  {
    name: $i18n.t("common.details"),
    type: "info",
    class: "row-btn-info",
    handleName: "detail" //点击时返回的标识
  },
  {
    name: $i18n.t("common.delete"),
    type: "danger",
    class: "row-btn-danger",
    handleName: "delete" //点击时返回的标识
  }
]
// 搜索条件配置
export const searchConfig = {
  inline: true,
  formDesc: {
    faultName: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.faultName")
    },
    isRepair: {
      type: "select",
      label: $i18n.t("repairMaterialsInfo.isRepair"),
      prop: {
        value: "value",
        text: "description"
      },
      options: async (data) => {
        const list = await getIsReviewList()
        return list.data.result
      }
    },
    orderCode: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.orderCode")
    },
    productProcessName: {
      type: "input",
      label: $i18n.t("common.processName")
    },
    prodName: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.prodName")
    },
    seriesName: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.seriesName")
    },
    equipmentName: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.equipmentName")
    },
    reviewCode: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.reviewCode")
    }
  }
}
// 新增/删除表单配置
export const formDescDialog = {
  data() {
    const _this = this
    return {
      formDescDialog: {
        span: 24,
        disabled: false,
        isShowSubmitBtn: true,
        formDesc: {
          repairWorkshop: {
            type: "input",
            label: $i18n.t("repairMaterialsInfo.repairWorkshop"),
            layout: 12,
            disabled: true,
            required: true,
          },
          repairLineName: {
            type: "input",
            label: $i18n.t("repairMaterialsInfo.repairLineName2"),
            layout: 12,
            disabled: true,
            required: true,
            vif: false
          },
          id: {
            type: "",
            label: "",
            layout: 1,
            labelWidth: "0",
            break: true
          },
          repairProcessCode: {
            type: "select",
            label: $i18n.t("repairMaterialsInfo.repairProcess"),
            layout: 12,
            required: true,
            optionsLinkageFields: ["id"],
            prop: {
              value: "processCode",
              text: "processName"
            },
            // Attr:{
            //   filterable: 'true'
            // },
            on: {
              change: () => {
                setTimeout(() => {
                  let { workCellName, equipmentName } = _this.formRow
                  _this.formDataDialog.workCellName = workCellName
                  _this.formDataDialog.equipmentName = equipmentName
                }, 0)
              }
            },
            options: async (data) => {
              let list = await getRepairProcessList({
                orderCode: data.orderCode,
                processCode: data.productProcessCode
              })
              optionList[0] = list.data.result
              return list.data.result
            },
            vif: (data) => {
              // 工单报工-统一报工 显示车间（必选）、设备（非必选）
              if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_unite") {
                return false
              }
              // 工单报工-按工序报工 显示车间（必选）、工序（必选）
              else if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_process") {
                return true
              }

              // 工序报工 显示车间（必选）、工序（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_process") {
                return true
              }

              // 工位报工 显示车间（必选）、工序（必选）、工位（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_workcell") {
                return true
              } else {
                return false
              }
            }
          },
          repairCellCode: {
            type: "select",
            label: $i18n.t("repairMaterialsInfo.repairCell"),
            layout: 12,
            prop: {
              value: "workCellCode",
              text: "workCellName"
            },
            // vif(data) {
            //   return data.repairProcessCode != data.productProcessCode;
            // },
            vif: (data) => {
              // 工单报工-统一报工 显示车间（必选）、设备（非必选）
              if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_unite") {
                return false
              }
              // 工单报工-按工序报工 显示车间（必选）、工序（必选）
              else if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_process") {
                return false
              }

              // 工序报工 显示车间（必选）、工序（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_process") {
                return false
              }

              // 工位报工 显示车间（必选）、工序（必选）、工位（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_workcell") {
                return true
              } else {
                return false
              }
            },
            required: (data) => {
              // 工位报工 显示车间（必选）、工序（必选）、工位（必选）、设备（非必选）
              if (data.reportType == "mes_report_type_workcell") {
                return true
              } else {
                return false
              }
            },
            optionsLinkageFields: ["repairProcessCode"],
            options: async (data) => {
              let list = { data: { result: [] } }

              if (data.repairProcessCode) {
                list = await getCellList({
                  processCode: data.repairProcessCode,
                  lineCode: data.lineCode
                })
              }

              optionList[1] = list.data.result
              return list.data.result
            }
          },
          repairEquipmentCode: {
            type: "input",
            label: $i18n.t("repairMaterialsInfo.repairEquipment"),
            layout: 12,
            prop: {
              value: "equipmentCode",
              text: "equipmentName"
            },
            // vif(data) {
            //   return data.repairProcessCode != data.productProcessCode;
            // },
            vif: (data) => {
              // 工单报工-统一报工 显示车间（必选）、设备（非必选）
              if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_unite") {
                return true
              }
              // 工单报工-按工序报工 显示车间（必选）、工序（必选）
              else if (data.reportType == "mes_report_type_order" && data.reportWay == "report_way_process") {
                return false
              }

              // 工序报工 显示车间（必选）、工序（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_process") {
                return true
              }

              // 工位报工 显示车间（必选）、工序（必选）、工位（必选）、设备（非必选）
              else if (data.reportType == "mes_report_type_workcell") {
                return true
              } else {
                return false
              }
            },
            optionsLinkageFields: ["repairCellCode"]
            // options: async (data) => {
            //   let list = { data: { result: [] } };
            //   // 123
            //   if (data.repairCellCode) {
            //     list = await commonBaseEquipmentModelingList({
            //       modelingCode: data.repairCellCode,
            //     });
            //   }
            //   optionList[2] = list.data.result;
            //   return list.data.result;
            // },
          },
          workCellName: {
            type: "input",
            label: $i18n.t("repairMaterialsInfo.workCellName"),
            layout: 12,
            disabled: true,
            vif(data) {
              // return data.repairProcessCode == data.productProcessCode;
              return false
            }
          },
          equipmentName: {
            type: "input",
            label: $i18n.t("repairMaterialsInfo.equipmentName2"),
            layout: 12,
            disabled: true,
            vif(data) {
              // return data.repairProcessCode == data.productProcessCode;
              return false
            }
          }
        }
      }
    }
  },
  methods: {
    doSubmit({ title, api, data }) {
      let { productProcessCode, productProcessName, workCellCode, workCellName, equipmentCode, equipmentName } =
        this.formRow

      let obj = {
        ...data,
        repairCellName: (
          (optionList[1] || []).find((ite) => ite.workCellCode == data.repairCellCode) || { workCellName: "" }
        ).workCellName,
        repairProcessName: (
          (optionList[0] || []).find((ite) => ite.processCode == data.repairProcessCode) || { processName: "" }
        ).processName
      }
      console.log("输出表单233232：", obj)

      // if (data.repairProcessCode != data.productProcessCode) {
      //   //  obj.repairProcessCode = productProcessCode;
      //   obj = {
      //     ...obj,
      //     repairProcessName: (
      //       (optionList[0] || []).find(
      //         (ite) => ite.processCode == data.repairProcessCode
      //       ) || { processName: "" }
      //     ).processName,

      //     repairCellName: (
      //       (optionList[1] || []).find(
      //         (ite) => ite.workCellCode == data.repairCellCode
      //       ) || { workCellName: "" }
      //     ).workCellName,

      //     repairEquipmentName: (
      //       (optionList[2] || []).find(
      //         (ite) => ite.equipmentCode == data.repairEquipmentCode
      //       ) || { equipmentName: "" }
      //     ).equipmentName,
      //   };
      // } else {
      //   obj = {
      //     ...obj,
      //     // 工序
      //     repairProcessCode: productProcessCode,
      //     repairProcessName: productProcessName,
      //     // 工位
      //     repairCellCode: workCellCode,
      //     repairCellName: workCellName,

      //     // 设备
      //     repairEquipmentCode: equipmentCode,
      //     repairEquipmentName: equipmentName,
      //   };
      // }
      this.isLoading = true

      api(obj)
        .then((res) => {
          const message = res.data.message
          this.$message.success(message || title)
          this.visible = false
          this.refresh()
        })
        .finally(() => {
          this.isLoading = false
        })
    }
  }
}
//导入数据
export const importFormDescDialog = {
  span: 24,
  disabled: false,
  isShowSubmitBtn: true,
  formDesc: {
    model: {
      type: "button",
      label: "",//$i18n.t("repairMaterialsInfo.model"),
      layout: 24,
      required: false,
      title: $i18n.t('common.downloadTemplate'),
      style: {
        color: "#1867c0",
        fontSize: "14px"
      },
      on: {
        click: () => {
          exportExcel(downloadTemplateUrl)
        }
      }
    },
    file: {
      type: "input",
      label: $i18n.t("repairMaterialsInfo.file"),
      layout: 24
      // required: true,
    }
  }
}
